package com.baomidou.plugin.idea.mybatisx.advance.processor.clazz;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiClassUtil;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiMethodUtil;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;

/**
 * 给po生成bean2Wrapper方法
 * @author wanglei
 */
public class Bean2WrapperProcessor extends AbstractMPOnceMethodGenClassProcessor {

  private static final String CLASS_NAME = TableName.class.getName();
  public static final String METHOD_NAME = "bean2Wrapper";

  public Bean2WrapperProcessor() {
    super(CLASS_NAME, PsiMethod.class);
  }

  @Override
  String getMethodName() {
    return METHOD_NAME;
  }

  @NotNull
  public Collection<PsiMethod> createToStringMethod(@NotNull PsiClass psiClass, @NotNull PsiElement psiNavTargetElement) {
    final PsiMethod[] classMethods = PsiClassUtil.collectClassMethodsIntern(psiClass);
    if (PsiMethodUtil.hasMethodByName(classMethods, METHOD_NAME)) {
      return Collections.emptyList();
    }

    final StringBuilder builder = new StringBuilder();
    try {

      builder.append("public com.baomidou.mybatisplus.core.conditions.query.QueryWrapper< ").append(psiClass.getName()).append(">")
              .append(METHOD_NAME).append("()");
      builder.append("{ return new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<" + psiClass.getName() + ">(); }");
      return Collections.singletonList(PsiMethodUtil.createMethod(psiClass, builder.toString(), psiNavTargetElement));
    } finally {
    }
  }

}
